<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\components;
use yii\caching\FileCache;
use app\components\pays\models\Flow;
/**
 * 用于管理更新缓存,需要配合后台进程
 *
 * @author shuai
 */
class CacheRedis {
    /**
     * 缓存开关
    */
   private $useCache=true;
   
   /**
    * 更新缓存的前缀名
   */
  private $qianzhui='cacheup_';

 
  public function __construct() {
        //   $this->useCache=false;
  }
  
  

  /**
    * 以下为缓存增加代码
   */
   const Incr_Flow='flow';
   
   public  function CacheIncrBy($tag,$id){
       if(!$this->useCache){
          
           return false;
       }
       if($tag===self::Incr_Flow){
           return $this->FlowUpdata($id);
       }
   }
   
   /**
    * 更新测试项的jilu值 
   */
  private function FlowUpdata($id){      
       $r=Lcms::Redis();
       $re=$r->zIncrBy($this->qianzhui.self::Incr_Flow,1, $id);
       if($re){
           return true;
       }else{
           return false;
       }
  }
  
   /**
    * 获取所有的键值
  */
 public function GetAllKeyName(){
     $r=  Lcms::Redis();  
     $keyName=$r->zRange($this->qianzhui.self::Incr_Flow,0,-1);
     return $keyName;/*
     print_r($keyName);exit;
     $ar=[];
     foreach ($keyName as $vo) {
         $ar[$vo]=$r->zScore($this->qianzhui.self::Incr_Flow, $vo);
          $r->zIncrBy($this->qianzhui.self::Incr_Flow, 0-5, $vo);
          
     } 
     print_r($ar);exit;*/
 }
    /**
     * 把值移动到数据库,并减去redis中的值
    */
    public function zhengli(){
        $r=  Lcms::Redis();
        foreach ($this->GetAllKeyName() as $vo) {
            $score=$r->zScore($this->qianzhui.self::Incr_Flow, $vo);
            $r->zIncrBy($this->qianzhui.self::Incr_Flow,0-$score, $vo);
            Flow::updateAllCounters(['jilu_value'=>$score],['id'=>$vo]);
        }
    }
    
}
